/*******************************************************************************
* Copyright (c) 2015 Pivotal Software, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Pivotal Software, Inc. - initial API and implementation
********************************************************************************/
package org.cloudfoundry.ide.eclipse.server.core.internal;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.cloudfoundry.client.lib.CloudFoundryOperations;
import org.cloudfoundry.client.lib.rest.CloudControllerResponseErrorHandler;
import org.cloudfoundry.ide.eclipse.server.core.internal.client.CloudFoundryServerBehaviour;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
/**
* Rest template for calls to Cloud Foundry that are typically external to the
* Cloud client (for example, directly sending requests to an application
* deployed in a Cloud space via the application URL that does not require Cloud
* Controller endpoints). For Cloud operations that go through the Cloud
* Controller, this should NOT be used. Instead, use API in
* {@link CloudFoundryServerBehaviour} which indirectly calls the underlying
* {@link CloudFoundryOperations}
*/
public class ExternalRestTemplate extends RestTemplate {
public ExternalRestTemplate() {
createRestTemplate();
}
protected ClientHttpRequestFactory createRequestFactory() {
HttpClientBuilder httpClientBuilder = HttpClients.custom().useSystemProperties();
HttpClient httpClient = httpClientBuilder.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
return requestFactory;
}
protected void createRestTemplate() {
setRequestFactory(createRequestFactory());
setErrorHandler(new CloudControllerResponseErrorHandler());
setMessageConverters(getHttpMessageConverters());
}
protected List<HttpMessageConverter<?>> getHttpMessageConverters() {
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
messageConverters.add(new ByteArrayHttpMessageConverter());
messageConverters.add(new StringHttpMessageConverter());
messageConverters.add(new ResourceHttpMessageConverter());
messageConverters.add(new MappingJackson2HttpMessageConverter());
return messageConverters;
}
}